﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Windows.Media.Imaging;

namespace Galaktika.BI.Silverlight.ClientApp
{
    using Galaktika.BI.Runtime;
    using Galaktika.BI.Extensibility;

    public class ClientResources
    {
        static Assembly m_Client;
        public static void Initialize(Assembly client)
        {
            m_Client = client;
        }

        public static Assembly ClientAssembly
        {
            get
            {
                return m_Client;
            }
        }

        public static bool IsInitialized
        {
            get
            {
                return m_Client != null;
            }
        }

        public static IBrickContainer GetContainer(string formId)
        {
            if (!IsInitialized)
            {
                throw new AppNotInitializedException();
            }

            if (string.IsNullOrEmpty(formId))
            {
                throw new ArgumentNullException("formId");
            }
            Type type = m_Client.GetType(formId);
            if (type != null)
            {
                IBrickContainer container = Activator.CreateInstance(type) as IBrickContainer;
                if (container != null)
                {
                    return container;
                }
            }

            return null;
        }

        public static FormView ConstructHostForm(FormView form, IBrickContainer container, string formId)
        {
            form.Id = formId;
            form.Initialize(container);

            return form;
        }

        private static Dictionary<string, string> m_KeyValues = null;
        private static void EnsureGlobalKeyValues()
        {
            if (!IsInitialized)
            {
                throw new AppNotInitializedException();
            }

            if (m_KeyValues == null)
            {
                m_KeyValues = new Dictionary<string, string>();

                GlobalKeyValueAttribute[] attrs =
                    (GlobalKeyValueAttribute[])m_Client.GetCustomAttributes(typeof(GlobalKeyValueAttribute), false);
                foreach (GlobalKeyValueAttribute attr in attrs)
                {
                    m_KeyValues.Add(attr.Key, attr.Value);
                }
            }
        }

        public static string GetGlobalValue(string key)
        {
            EnsureGlobalKeyValues();

            string value;
            m_KeyValues.TryGetValue(key, out value);

            return value;
        }

        public class Images
        {
            private const string ERROR_16 = "/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Aborted.png";
            public static BitmapImage Error16
            {
                get
                {
                    return GetImage(ERROR_16);
                }
            }

            private static BitmapImage GetImage(string resourceName)
            {
                return new BitmapImage(new Uri(resourceName, UriKind.Relative)); 
            }

            public const string RUNNING_16 = "/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Running.png";
            public static BitmapImage Running16
            {
                get
                {
                    return GetImage(RUNNING_16);
                }
            }

            public const string DONE_16 = "/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Done.png";
            public static BitmapImage Done16
            {
                get
                {
                    return GetImage(DONE_16);
                }
            }

            public const string ABORTED_16 = "/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Aborted.png";
            public static BitmapImage Aborted16
            {
                get
                {
                    return GetImage(ABORTED_16);
                }
            }
        }
    }
}
